﻿using System.Collections.Generic;
using Dataweb.NShape.Advanced;
using Dataweb.NShape;
using System.ComponentModel;
using cz.zcu.PreSti.Components.Sample;
using System;

namespace cz.zcu.PreSti.Shapes.Sample
{
    public class SampleStimulusSymbol : StimulusSymbol
    {
        protected override void InitializeToDefault(IStyleSet styleSet)
        {
            base.InitializeToDefault(styleSet);
            Text = "Sample";
        }

        public override void CopyFrom(Shape source)
        {
            base.CopyFrom(source);
        }

        public override int SettersCount
        {
            get { return 1; }
        }

        public override int GettersCount
        {
            get { return 1; }
        }

        public override string GetTitleForSetter(int id)
        {
            string tmp = base.GetTitleForSetter(id);
            switch (id)
            {
                case 0: tmp = "Delay"; break;
            }
            return tmp;
        }

        public override string GetTitleForGetter(int id)
        {
            string tmp = base.GetTitleForGetter(id);
            switch (id)
            {
                case 0: tmp = "Delay"; break;
            }
            return tmp;
        }

        public override Shape Clone()
        {
            Shape result = new SampleStimulusSymbol(Type, (Template)null);
            result.CopyFrom(this);
            return result;
        }

        protected internal SampleStimulusSymbol(ShapeType shapeType, Template template)
            : base(shapeType, template)
        {
        }

        #region ISymbol Members

        public override BaseComponent initializeComponent(IPresentation p)
        {
            SampleStimulusModel model = (SampleStimulusModel)this.ModelObject;
            SampleStimulus stimulus = new SampleStimulus(p);
            stimulus.Delay = model.Duration;
            return stimulus;
        }

        #endregion

        internal static Shape CreateInstance(ShapeType shapeType, Template template)
        {
            return new SampleStimulusSymbol(shapeType, template);
        }
    }

    public class SampleStimulusModel : ModelObjectBase
    {
        #region Fields

        private int duration;

        private const int PropertyIdDuration = 1;

        private char securityDomainName = 'A';

        #endregion

        #region properties

        [Category("Presentation")]
        [Description("Duration of the stimulus in miliseconds.")]
        [PropertyMappingId(PropertyIdDuration)]
        public virtual int Duration
        {
            get
            {
                return duration;
            }
            set
            {
                duration = value;
            }
        }

        #endregion

        public static SampleStimulusModel CreateInstance(ModelObjectType modelObjectType) {
			return new SampleStimulusModel(modelObjectType);
		}


		protected internal SampleStimulusModel(ModelObjectType modelObjectType)
			: base(modelObjectType) {
            this.terminalCount = 5;
		}


        protected internal SampleStimulusModel(SampleStimulusModel source)
			: base(source) {
            Duration = source.Duration;
            this.terminalCount = 5;
		}


		public override IModelObject Clone() {
            return new SampleStimulusModel(this);
		}


		public override IEnumerable<MenuItemDef> GetMenuItemDefs() {
			throw new NotImplementedException();
		}


		public override void Connect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId) {
			throw new NotImplementedException();
		}


		public override void Disconnect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId) {
			throw new NotImplementedException();
		}


        public override char SecurityDomainName
        {
            get { return securityDomainName; }
            set
            {
                if (value < 'A' || value > 'Z')
                    throw new ArgumentOutOfRangeException("SecurityDomainName", "The domain qualifier has to be an upper case  ANSI letter (A-Z).");
                securityDomainName = value;
            }
        }


        #region IEntity Members

        public static new IEnumerable<EntityPropertyDefinition> GetPropertyDefinitions(int version)
        {
            foreach (EntityPropertyDefinition pi in ModelObjectBase.GetPropertyDefinitions(version))
                yield return pi;
            yield return new EntityFieldDefinition("Duration", typeof(int));
        }

        protected override void LoadFieldsCore(IRepositoryReader reader, int version)
        {
            base.LoadFieldsCore(reader, version);
            Duration = reader.ReadInt32();
        }

        protected override void SaveFieldsCore(IRepositoryWriter writer, int version)
        {
            base.SaveFieldsCore(writer, version);
            writer.WriteInt32(Duration);
        }

        #endregion

    }

}

